import {GlobalVariables} from "../GlobalVariables";
import axios from 'axios';
import {ElMessage} from 'element-plus';

GlobalVariables.axios = axios.create();

export class AxiosCtrl {
    public static async searchByStr(addr: string) {
        const url = `https://restapi.amap.com/v5/place/text`;
        const params = {
            key: "80a70e21f47611e3bc7379ceb6596935",
            city: "福州市",
            citylimit: true,
            page_num: 1,
            page_size: 10,
            keywords: addr
        }
        const res = await GlobalVariables.axios.get(url, {params});
        const data = res.data;
        if (data.status == "0") {
            ElMessage.error("服务请求错误");
            return [];
        }
        return data.pois;
    }
}

//爬取高德数据
async function getData() {
    const result = [];
    let count = 25;
    let index = 1;
    for (; count > 0;) {
        const url = `https://restapi.amap.com/v5/place/text?key=80a70e21f47611e3bc7379ceb6596935&region=%E7%A6%8F%E5%B7%9E&types=150900|150903|150904|150905|150906|150907|150908|150909&page_size=25&page_num=${index}`;
        const res = await GlobalVariables.axios.get(url);
        const data = res.data;
        if (data.status == 0) {
            continue;
        }
        index++;
        count = data.pois.length;
        if (count != 0) {
            result.push(...data.pois);
        }
        if (index > 100) {
            count = 0;
        }
    }
    console.log(result);
    console.log(JSON.stringify(result));
}

async function ssss() {
    const res = await GlobalVariables.axios.get("./json/bike.json");
    const data = res.data;
    console.log(data);
    const result = data.map((item: any) => {
        const location = item.location;
        return {
            "name": item.name,
            "id": item.uid,
            "location": location.lng +","+ location.lat,
            "type": item.detail_info.tag,
            "typecode": "150904",
            "pname": item.province,
            "cityname": item.city,
            "adname": item.area,
            "address": item.address,
            "pcode": "350000",
            "citycode": "0591",
            "adcode": "350111"
        }
    })
    console.log(result);
    console.log(JSON.stringify(result));
}
